package test.hxy.mongoSchoolSimple.controller;

import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.*;
import test.hxy.mongoSchoolSimple.model.Department;
import org.springframework.beans.factory.annotation.Autowired;
import test.hxy.mongoSchoolSimple.model.Leader;
import test.hxy.mongoSchoolSimple.service.DepartmentService;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.Stream;

@RestController
@RequestMapping("/department")
public class DepartmentController {
	@Autowired
	DepartmentService departmentService;

	@RequestMapping("/getByVacanciesBetween")
	public List<Department> getVacBet(@RequestParam Integer num1,@RequestParam Integer num2) {
		return departmentService.getDepartmentByVacanciesBetween(num1, num2);
	}

	@RequestMapping("/getByHeadcountLessThanOrHeadcountGreaterThan")
	public List<Department> getheadcount(@RequestParam Integer num1,@RequestParam Integer num2) {
		return departmentService.getDepartmentByHeadcountLessThanOrHeadcountGreaterThan(num1, num2);
	}

	@RequestMapping("/getByNameIn")
	@ResponseBody
	public List<Department> getNameIn(@RequestBody List<String> names) {
		return departmentService.getDepartmentByNameIn(names);
	}

	@RequestMapping("/getByTypeIn")
	public List<Department> getTypeIn(@RequestParam Integer type,@RequestParam int page,@RequestParam int size) {
		return departmentService.getDepartmentByTypeIn(type, page, size);
	}
	@RequestMapping("/getAllDepartment")
	public List<Department> getAll(){
		return departmentService.getAll();
	}

	@RequestMapping("/getSubAll")
	@ResponseBody
	public List<Department> getSubAll(@RequestBody String[] names) {
		return departmentService.getDepartmentBysub(names);
	}

	@RequestMapping("/getByleaders")
	public List<Department> getleaders(@RequestParam String position,@RequestParam String title) {
		return departmentService.getDepartmentByleaders(position, title);
	}

	@RequestMapping("/insertDepartment")
	public Map<String, String> insertDepartment(HttpServletRequest request) {
		Department department = new Department();
		department.setId(request.getParameter("id"));
		department.setName(request.getParameter("name"));
		department.setType(Integer.parseInt(request.getParameter("type")));
		department.setHeadcount(Integer.parseInt(request.getParameter("headcount")));
		department.setLeaders(null);
		department.setSubdepartment(request.getParameter("subdepartment").split(","));
		department.setVacancies(Integer.parseInt(request.getParameter("vacancies")));
		return departmentService.insertDepartment(department);
	}
	@RequestMapping("/updateLeader")
	public Map<String, String> updateLeader(HttpServletRequest request) {
		Leader leader = new Leader();
		leader.setName(request.getParameter("name"));
		leader.setPosition(request.getParameter("position"));
		leader.setTitle(request.getParameter("title"));
		leader.setTel(request.getParameter("tel"));
		leader.setOffice(request.getParameter("office"));
		leader.setDuty(request.getParameter("duty"));
		String id = request.getParameter("_Id");
		return departmentService.upsertLeader(leader, id);
	}

	@RequestMapping("/deleteDepartment")
	public Map<String, String> deleteDepartment(@RequestParam String name) {
		return departmentService.deleteDepartment(name);
	}
}
